package com.mama100.rpc.mdp.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Properties;

import javax.annotation.Resource;
import javax.naming.InitialContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.it.common.SystemTypeEnum;
import com.mama100.common.util.general.GlobalConfigUtil;
import com.mama100.coupon.inout.BatchIssueCouponRequest_4_3_0;
import com.mama100.coupon.inout.BatchIssueCouponResponse;
import com.mama100.coupon.inout.CouponConditionEnum;
import com.mama100.coupon.inout.CouponIssueInfo;
import com.mama100.coupon.inout.CouponResponseEnum;
import com.mama100.coupon.inout.IssueCouponRequest_4_3_0;
import com.mama100.coupon.inout.IssueCouponResponse;
import com.mama100.coupon.service.CouponService;
import com.mama100.rpc.mdp.service.ICouponService;

@Service
public class ICouponServiceImpl implements ICouponService {
	protected Logger log = LoggerFactory.getLogger(this.getClass());

	@Resource(name = "racJdbcTemplate")
	JdbcTemplate jdbcTemplate;

	private InitialContext ctx;

	private CouponService couponService;

	public ICouponServiceImpl() {
		initRemote();
	}

	/**
	 * 初始化优惠券接口
	 */
	private void initRemote() {
		Properties props = GlobalConfigUtil.getInstance().getPropertiesByEnv("coupon.properties");
		try {
			ctx = new InitialContext(props);
			couponService = (CouponService) ctx.lookup("java:global/couponcampaign/CouponServiceImpl");
			log.info("init remote ok!!!!");
		} catch (Exception e) {
			log.error("init remote error", e);
		}
	}

	/**
	 * 发送优惠券
	 * 
	 * @param customerId
	 * @param couponDefId
	 * @param couponConditionValue
	 */
	public void sendCoupin(Long customerId, Long couponDefId, String couponConditionValue) throws Exception {

		couponService = (CouponService) ctx.lookup(CouponService.class.getName());
		IssueCouponRequest_4_3_0 request = new IssueCouponRequest_4_3_0();
		request.setSeqNo(makeDateSeqNo());
		request.setSourceSystem(SystemTypeEnum.SYSTEM);
		request.setCouponCondition(CouponConditionEnum.CAMPAIGN);
		request.setCouponDefId(couponDefId);
		request.setCouponConditionValue(couponConditionValue);

		request.setCustomerId(customerId);// 实际传入的顾客id

		log.info(JSON.toJSONString(request));

		IssueCouponResponse response = couponService.issueCoupon_4_3_0(request);

		String retmsg = JSON.toJSONString(response);

		log.info("remote return:" + retmsg);

		if (response.getCouponResponseEnum().equals(CouponResponseEnum.COUPON_SUCCESS)) {
			return;
		} else {
			throw new Exception(retmsg);
		}

	}
	
	/**
	 * 发送优惠券(批量)
	 * 
	 * @param customerId
	 * @param couponDefId
	 * @param couponConditionValue
	 */
	public void sendbatchCoupin(Long customerId, List<CouponIssueInfo> infos) throws Exception {

		couponService = (CouponService) ctx.lookup(CouponService.class.getName());

		BatchIssueCouponRequest_4_3_0 request = new BatchIssueCouponRequest_4_3_0();
		
		
		request.setSeqNo(makeDateSeqNo());
		request.setCustomerId(customerId);// 实际传入的顾客id
		request.setCouponIssueInfos(infos);
		request.setSourceSystem(SystemTypeEnum.SYSTEM);
		
		/*request.setCouponCondition(CouponConditionEnum.CAMPAIGN);
		request.setCouponDefId(couponDefId);
		request.setCouponConditionValue(couponConditionValue);*/

		log.info(JSON.toJSONString(request));

		BatchIssueCouponResponse response = couponService.batchIssueCoupon_4_3_0(request);

		String retmsg = JSON.toJSONString(response);

		log.info("remote return:" + retmsg);

		if (response.getCouponResponseEnum().equals(CouponResponseEnum.COUPON_SUCCESS)) {
			return;
		} else {
			throw new Exception(retmsg);
		}

	}
	

	private String makeDateSeqNo() {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddhhmmss");
		Date now = new Date();
		String dateStr = format.format(now) + "" + now.getTime();
		return dateStr;
	}

}
